function go2URL(address,target){
    window.open(address,target);
}

function newWindow(address){
    var maxW = screen.width;
    var maxH = screen.height;
    var w = 800;
    var h = 600;
    var _top = Math.floor((maxH - h) / 2);
    var _left = Math.floor((maxW - w) / 2);

    var win = window.open(address,'doiW',"toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width="+w+",height="+h);
    win.moveTo(_left,_top);
    win.focus();
}

function isInt(v){
    return (parseInt(v) == v);
}

function detectNum(e){
   var nv='';
   var pt=false;
   for (x=0; x<e.value.length; x++)
   {
     c = e.value.substring(x,x+1);
     if (isInt(c) || ((c == '.') && (pt == false)) || ((x == 0) && (c == '-')))
   {
   nv += c;
   if (c == '.') { pt=true; }
   }
  }
   e.value = nv;
}

function isInteger(num){
 return (!isNaN(num) && num%1==0);
}

/**
 * Check whether number or not from pressing keyboard
 *
 * @author  Arief Sofyan
 * @date    11.10.2008 09:11:12
 * @param   event
 * @use     onkeyup="javascript:return numberOnly(event);"
 */
function numberOnly(e) {
    var keycode;

    if (window.event) keycode = window.event.keyCode;
      else if (e) keycode = e.which;

    // 48..57  => 0..9
    // 8       => Backspace
    // 190     => . (for decimal)
    // 37..40  => Arrow Key
    // 9       => Tab
    // 46      => Del
    // 96..105 => (NumPad) 0..9
    // 110     => (NumPad) . (for decimal)

    if ((keycode >= 48 && keycode <= 57) ||
        (keycode >= 37 && keycode <= 40) ||
        (keycode >= 96 && keycode <= 105) ||
         keycode==8 ||
         keycode==190 ||
         keycode==9 ||
         keycode==110 ||
         keycode==46) {
      return true;
    } else {
      return false;
    }
}

/**
 * This function is to create a Thickbox
 *
 * @author  Arief Sofyan
 * @date    Mon, Dec 18, 200613.10.2008 11:03:15
 * @param   string
 * @param   string
 * @param   string
 * @param   string
 * @param   string
 */
function makeTdThickbox(theId, searchName, urlSite, imageUrl, align) {
    td1 = document.createElement("td");
    td1.setAttribute('class', 'FacetColumnTD');
    td1.setAttribute('align', align);

    div1 = document.createElement("a");
    div1.setAttribute('href', urlSite);
    div1.setAttribute('id', theId);
    div1.setAttribute('name', searchName);
    div1.setAttribute('class', 'thickbox');

    img1 = document.createElement("img");
    img1.setAttribute('src', imageUrl);
    img1.setAttribute('alt', 'search');

    div1.appendChild(img1);
    td1.appendChild(div1);
    return td1;
}

function makeTdThickboxWithDiv(theId, searchName, urlSite, imageUrl, align, divId, divName) {
    td1 = document.createElement("td");
    td1.setAttribute('class', 'FacetColumnTD');
    td1.setAttribute('align', align);

    div1 = document.createElement("a");
    div1.setAttribute('href', urlSite);
    div1.setAttribute('id', theId);
    div1.setAttribute('name', searchName);
    div1.setAttribute('class', 'thickbox');

    img1 = document.createElement("img");
    img1.setAttribute('src', imageUrl);
    img1.setAttribute('alt', 'search');

    txt1 = document.createElement("div");
    txt1.setAttribute('id', divId);
    txt1.setAttribute('name', divName);
    txt1.setAttribute('class', "inlinediv");

    div1.appendChild(img1);
    td1.appendChild(div1);
    td1.appendChild(txt1);
    return td1;
}

function makeTdThickboxFull(items, name, size, style, urlSite, imageUrl, align, a_name, a_class) {
    td1 = document.createElement("td");
    td1.setAttribute('class', 'FacetColumnTD');
    td1.setAttribute('align', align);

    div1 = document.createElement("input");
    div1.setAttribute('type', 'text');
    div1.setAttribute('class', 'input');
    div1.setAttribute('id', name+'_code_'+items);
    div1.setAttribute('name', name+'_code_'+items);
    div1.setAttribute('size', size);
    div1.setAttribute('readonly', 'readonly');

    div2 = document.createElement("a");
    div2.setAttribute('href', urlSite);
    div2.setAttribute('id', a_name);
    div2.setAttribute('class', 'thickbox '+a_class);

    img1 = document.createElement("img");
    img1.setAttribute('src', imageUrl);
    img1.setAttribute('alt', 'search');

    div2.appendChild(img1);

    div3 = document.createElement("input");
    div3.setAttribute('type', 'text');
    div3.setAttribute('class', 'input');
    div3.setAttribute('id', name+'_name_'+items);
    div3.setAttribute('name', name+'_name_'+items);
    div3.setAttribute('style', style);
    div3.setAttribute('readonly', 'readonly');

    td1.appendChild(div1);
    td1.appendChild(div2);
    td1.appendChild(div3);
    return td1;
}

function makeTdHidden(id, inName, inVal, inSize, class__) {
   if(!class__) {
        class__ = "";
   }

   div1 = document.createElement("input");
   div1.setAttribute('type', 'hidden');
   div1.setAttribute('id', id);

   div1.setAttribute('name', inName);
   div1.setAttribute('size', inSize);
   div1.setAttribute('class', class__);
   div1.value=inVal;
   return div1;
}

function makeTdDiv(id, textVal, align) {
   td1 = document.createElement("td");
   td1.setAttribute("class", "FacetColumnTD");
   td1.setAttribute("align", align);
   div1 = document.createElement("div");
   div1.setAttribute('id', id);
   div1.innerHTML=textVal;
   td1.appendChild(div1);
   return td1;
}
function makeTdDoble(tdID,id,theId,inName,inSize,inMax,inType,inVal,selData,selValue,align){
    td1 = document.createElement("td");
    td1.setAttribute("id",tdID);
    td1.setAttribute("class", "FacetColumnTD");
    td1.setAttribute("align", align);
    selek1 = document.createElement("select");
    selek1.setAttribute('id', id);
    selek1.setAttribute('name', inName);
    selek1.setAttribute('class', "input");
   for (i=0; i<selData.length; i++) {
      selek1.options[i]=new Option(selData[i], selValue[i]);
   }
   td1.appendChiled(selek1);
   div1 = document.createElement("input");
   div1.setAttribute('type', inType);
   div1.setAttribute('class', 'input');
   div1.setAttribute('id', theId);
   div1.setAttribute('name', inName);
   div1.setAttribute('size', inSize);
   div1.setAttribute('maxlength', inMax);
   div1.value=inVal;
   td1.appendChild(div1);
   return td1;
}
function makeTdInput(inType, theId, inName, inVal, inSize, inMax, align, style) {
   td1 = document.createElement("td");
   td1.setAttribute("class", "FacetColumnTD");
   td1.setAttribute("align", align);
   div1 = document.createElement("input");
   div1.setAttribute('type', inType);
   div1.setAttribute('class', 'input');
   div1.setAttribute('id', theId);
   div1.setAttribute('name', inName);
   div1.setAttribute('size', inSize);
   div1.setAttribute('maxlength', inMax);
   div1.setAttribute('style', style);
   div1.value=inVal;
   td1.appendChild(div1);
   return td1;
}
function makeTdInputOnClick(inType, theId, inName, inVal, inSize, inMax, align, style, onclick) {
   td1 = document.createElement("td");
   td1.setAttribute("class", "FacetColumnTD");
   td1.setAttribute("align", align);
   div1 = document.createElement("input");
   div1.setAttribute('type', inType);
   div1.setAttribute('class', 'input');
   div1.setAttribute('id', theId);
   div1.setAttribute('name', inName);
   div1.setAttribute('size', inSize);
   div1.setAttribute('maxlength', inMax);
   div1.setAttribute('style', style);
   div1.setAttribute('onblur', onclick);
   div1.value=inVal;
   td1.appendChild(div1);
   return td1;
}
function makeTdInputReadOnly(inType, theId, inName, inVal, inSize, inMax, align, style, readonly) {
   td1 = document.createElement("td");
   td1.setAttribute("class", "FacetColumnTD");
   td1.setAttribute("align", align);
   div1 = document.createElement("input");
   div1.setAttribute('type', inType);
   div1.setAttribute('class', 'input');
   div1.setAttribute('id', theId);
   div1.setAttribute('name', inName);
   div1.setAttribute('size', inSize);
   div1.setAttribute('maxlength', inMax);
   div1.setAttribute('style', style);
   div1.setAttribute('readonly', readonly);
   div1.value=inVal;
   td1.appendChild(div1);
   return td1;
}

function makeTdCheck(id, inName, onClickAction, align, class__) {
   /**
    * 3rd Parameter (onClickAction) added by Sugeng Utomo
    */
   if(!class__) {
        class__ = "";
   }
   td1 = document.createElement("td");
   td1.setAttribute("class", "FacetColumnTD");
   td1.setAttribute("align", align);
   div1 = document.createElement("input");
   div1.setAttribute('type', 'checkbox');
   div1.setAttribute('name', inName);
   div1.setAttribute('class', class__);

   /**
    * You've forgotten something to set an attribute for ID to input Element
    *
    * @author   Sugeng Utomo
    * @date Fri, Dec 08, 2006
    * @time 03:50 PM
    */
   div1.setAttribute('id', id);

   /**
    * Add an action for event onClick()
    *
    * @author   Sugeng Utomo
    * @date     Fri, Dec 08, 2006
    * @time     04:52 PM
    */
   if (onClickAction!="") div1.setAttribute('onclick', onClickAction);

   td1.appendChild(div1);
   return td1;
}

function makeTdSelect(id, inName, selData, selValue, align, style) {
   td1 = document.createElement("td");
   td1.setAttribute("class", "FacetColumnTD");
   td1.setAttribute("align", align);
   div1 = document.createElement("select");
   div1.setAttribute('id', id);
   div1.setAttribute('name', inName);
   div1.setAttribute('class', "input");
   div1.setAttribute('style', style);
   for (i=0; i<selData.length; i++) {
      div1.options[i]=new Option(selData[i], selValue[i]);
   }
   td1.appendChild(div1);
   return td1;
}
function makeTdSelectEvent(id, inName, selData, selValue, align, style,event) {
   td1 = document.createElement("td");
   td1.setAttribute("class", "FacetColumnTD");
   td1.setAttribute("align", align);
   div1 = document.createElement("select");
   div1.setAttribute('id', id);
   div1.setAttribute('name', inName);
   div1.setAttribute('class', "input");
   div1.setAttribute('style', style);
   div1.setAttribute('onChange', event);
   for (i=0; i<selData.length; i++) {
      div1.options[i]=new Option(selData[i], selValue[i]);
   }
   td1.appendChild(div1);
   return td1;
}

function checkAllByPrefix(items, objCheckBox, checkBoxPrefix) {
/**
 * REMEMBER THAT :
 *    objCheckBox refers to checkBox with caption "CHECK ALL" or "CANCEL ALL"
 */
var state = (objCheckBox.checked) ? true : false;

for (n=1; n <= items; n++)
  if (document.getElementById(checkBoxPrefix + n))
    document.getElementById(checkBoxPrefix + n).checked = state;
}

function getNextElementId(what, parentNode, tagName) {
    var element        = document.getElementById(parentNode);
    var startToProcess = false;

    for (i = 0; i < element.childNodes.length; i++) {
        if (element.childNodes[i].tagName==tagName) {
            if (startToProcess) return element.childNodes[i].getAttribute('id');
            if (element.childNodes[i].getAttribute('id')==what) startToProcess = true;
        }
    }

    return '';
}

function thisIdHasSub(candID, list) {
  var nextID  = getNextElementId(candID, 'maincontent'+list, 'tr');

  if (nextID.indexOf('sub') != -1) {
    return true;
  } else {
    return false;
  }
}

function clearAllSub(candID) {
  var i = 1;
  var sequence = 0;
  while (i < 566) {
    if (document.getElementById(candID + '_sub' + i)) {
      document.getElementById('maincontent').removeChild(document.getElementById(candID + '_sub' + i));
      i++;
    } else {
      i = 566;
    }
  }
}

/**
 *  rowPrefix => prefix dari id <tr>
 *  checkBoxPrefix => prefix dari checkBox yang d gunakan
 *  item => nama var item pada fungsi AddItem
 *  list => prefix dari maincontent -> <tbody id="maincontent">
 */
function removeRowsByCheckedBox(items, rowPrefix, checkBoxPrefix, list) {
    var result = true;
    if(!list)
        list='';

    for (n=1; n<=items; n++) {
      if (document.getElementById(rowPrefix + n)) {
        if (document.getElementById(checkBoxPrefix + n).checked==true) {
          // Clear Sub if there is
          if (thisIdHasSub(rowPrefix + n, list)) clearAllSub(rowPrefix + n, list);

          document.getElementById(rowPrefix + n).parentNode.removeChild(document.getElementById(rowPrefix + n));

          //for (var idx in textBoxID[tp1.getSelectedIndex()])
          //  if (textBoxID[tp1.getSelectedIndex()][idx] == n) textBoxID[tp1.getSelectedIndex()].splice(idx,1);
        }
      }
    }
}

function removeRowsByCheckedBoxDefine(items, rowPrefix, checkBoxPrefix, list) {
    var cek = 0;
    for (n=1; n <= items; n++) {
        if (document.getElementById(rowPrefix + n)) {
            if (document.getElementById(checkBoxPrefix + n).checked==true) {
                cek++;
            }
        }
    }
    if(cek>0){
        if(confirm('Are you sure you want to delete?')){
            var result = true;
            if(!list)
                list='';

            for (n=1; n <= items; n++) {
              if (document.getElementById(rowPrefix + n)) {
                if (document.getElementById(checkBoxPrefix + n).checked==true) {
                  if (thisIdHasSub(rowPrefix + n, list)) clearAllSub(rowPrefix + n, list);
                    document.getElementById(rowPrefix + n).parentNode.removeChild(document.getElementById(rowPrefix + n));
                }
              }
            }
            document.getElementById(checkBoxPrefix + 'all').checked=false;
        }
    }
}

//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/geral/event-listener [rev. #5]
/**
 * addEvent(object: Object, event: String, handler: Function(e: Event): Boolean, [scope: Object = object]): void
 * Adds an event listener to an object.
 *
 * removeEvent(object: Object, event: String, handler: function(e: Event): Boolean, [scope: Object = object]): Boolean
 * Removes a previously added listener from the object and returns true in case of success
 */
addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};

//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/forms/format-currency [rev. #3]
/**
 * formatCurrency(field: HTMLInput, [floatPoint: Integer = 2], [thousandsSep: String = "."], [decimalSep: String = ","]): String
 * Formats the input making it assume the behaviour of a monetary field
 */
function formatCurrency(o, n, dig, dec){
    new function(c, dig, dec, m){
        addEvent(o, "keypress", function(e, _){
            if((_ = e.key == 45) || e.key > 47 && e.key < 58){
                var o = this, d = 0, n, s, h = o.value.charAt(0) == "-" ? "-" : "",
                    l = (s = (o.value.replace(/^(-?)0+/g, "$1") + String.fromCharCode(e.key)).replace(/\D/g, "")).length;
                m + 1 && (o.maxLength = m + (d = o.value.length - l + 1));
                if(m + 1 && l >= m && !_) return false;
                l <= (n = c) && (s = new Array(n - l + 2).join("0") + s);
                for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += dig);
                n && n < l && (s[l - ++n] += dec);
                _ ? h ? m + 1 && (o.maxLength = m + d) : s[0] = "-" + s[0] : s[0] = h + s[0];
                o.value = s.join("");
            }
            e.key > 30 && e.preventDefault();
        });
    }(!isNaN(n) ? Math.abs(n) : 2, typeof dig != "string" ? "." : dig, typeof dec != "string" ? "," : dec, o.maxLength);
}

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/number/fmt-money [rev. #2]
/**
 * Number.formatMoney([floatPoint: Integer = 2], [decimalSep: String = ","], [thousandsSep: String = "."]): String
 * Returns the number into the monetary format.
 */
Number.prototype.formatMoney = function(c, d, t){
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)
    + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00'
              : ( (mnt*10 == Math.floor(mnt*10)) ?
                       mnt + '0' : mnt);
};

/* add by Chandra ( 28 Mei 2009 )
 * purpose: Disbursement check all list
 * */

function dsbCheckAll(status){
    $('.dsbCheck').each(
      function(){
        $(this).attr('checked',status?'checked':'');
      }
    );
}

function setAmount(element_name){
    var total = 0;
    $('.dsbCheck').each(
      function(){
        if($(this).attr('checked')){
          total += parseFloat($('input[@name='+element_name+$(this).val()+']').val());
        }
      }
    )
    $('#totalRefference').text('USD '+total.formatMoney());
}


// add by chandra (5 june 2009)
function getDateClient(format){
    var format = format || 1;
    var d = new Date();
    var mt = d.getMonth()+1;
    var day = d.getDate() < 10 ? '0'+d.getDate() : d.getDate();
    var mon = mt < 10 ? '0'+mt : mt;
    return (format==1 ? day+'/'+mon+'/'+d.getFullYear() : d.getFullYear()+'-'+mon+'-'+day);
}

// add by chandra (12 oct 2009)
function setTarget(button,ftarget){
	var ftarget = ftarget || 1;
	if(ftarget != 1) window.open('../../_blank.html','pdfBlank','width=700,height=500');
	button.form.target = (ftarget == 1) ? '_self' : 'pdfBlank';
}
